// pages/ledger/month/index.js
var fileManager = require('../../../libs/fileManager');
Page({

	/**
	 * 页面的初始数据
	 */
	data: {
		list: [],
		longPressIndex: null,
		totalMoney: 0,

		refresherTriggered: false,
		monthListDic: ["一", "二", "三", "四", "五", "六", "七", "八", "九", "十", "十一", "十二"],
		monthName: "本",
		dateMonthIdx: "",
		dateMonthList: []
	},

	/**
	 * 生命周期函数--监听页面加载
	 */
	onLoad(options) {
		// 获取当前月份
		let nowDate = new Date().getMonth()
		this.setData({
			monthName: this.data.monthListDic[nowDate]
		})
		this.getMonthFileList()
	},
	onShow() {
		this.getList(this.data.dateMonthList[this.data.dateMonthIdx])
	},


	// 选择月份
	bindPickerChange(e) {
		this.data.dateMonthIdx = e.detail.value
		console.log("选择月份文件", this.data.dateMonthList[this.data.dateMonthIdx])
		let year_month = this.data.dateMonthList[this.data.dateMonthIdx]
		let monthCheck = Number(year_month.split("-")[1]) - 1
		this.setData({
			monthName: this.data.monthListDic[monthCheck]
		})
		this.getList(this.data.dateMonthList[this.data.dateMonthIdx])
	},

	// 获取所有月份文件列表
	async getMonthFileList() {
		let list = await fileManager.getFileList();
		console.log("查看当前缓存月份文件列表", list)
		list = list.filter(itemMap => !["miniprogramLog"].includes(itemMap))
		this.setData({
			dateMonthList: [...list]
		})
		console.log("获取所有月份列表", this.data.dateMonthList)
		// 设置默认月份选中值
		if (['', false, null].includes(this.data.dateMonthIdx)) {
			let nowMonth = new Date().getMonth()
			list.forEach((item, index) => {
				let listMon = item.split("-")[1]
				if (nowMonth == listMon - 1) {
					this.setData({
						dateMonthIdx: index
					})
					console.log("设置默认月份选中值", this.data.dateMonthIdx)
				}
			})
		}
	},
	// 操作本条数据
	getDayData(event) {
		let index = event.target.dataset.index
		wx.showActionSheet({
			itemList: ['删除', '详情'],
			success: res => {
				if (res.tapIndex === 0) {
					this.deleteItem(index)
				} else {
					this.goDetail(index)
				}
			},
			fail(res) {
				console.log(res.errMsg)
			}
		})
	},

	// 下拉刷新事件被触发
	bindrefresherrefresh() {
		this.getList(this.data.dateMonthList[this.data.dateMonthIdx])
	},
	// 获取本月账单
	getList(fileMonth) {
		fileManager.sumMonthFileJS(fileMonth).then(totalFileName => {
			console.log("准备获取合计文件内数据", totalFileName)
			fileManager.getTodayDataJS(totalFileName, fileMonth).then(res => {
				this.setData({
					totalMoney: 0
				})
				res = res.data
				console.warn("查看月份账单文件,准备统计本月数据花销", res)
				res.forEach(resDItem => {
					this.setData({
						totalMoney: Number(this.data.totalMoney) + Number(resDItem.totalMoney)
					})
				})

				fileManager.getMonthFileListJS(fileMonth).then(monthFileList => {
					this.setData({
						list: monthFileList.filter(item => item.indexOf("合计") == -1),
						refresherTriggered: false
					})
				})
			}).catch(err => {
				console.error(err)
				this.setData({
					refresherTriggered: false
				})
			})
		}).catch(err => {
			console.error(err)
			this.setData({
				list: [],
				refresherTriggered: false
			})
		})
	},
	goShare() {
		let that = this
		let monthFileName = false
		if (!['', null, undefined, false].includes(this.data.dateMonthIdx)) {
			monthFileName = this.data.dateMonthList[this.data.dateMonthIdx]
		}
		wx.showActionSheet({
			itemList: ["分享" + monthFileName + '账单', "清理缓存文件"],
			success: (res) => {
				if (res.tapIndex === 0) {
					fileManager.shareMonthFileJS(monthFileName)
				}

				if (res.tapIndex === 1) {
					fileManager.removeFileDirStorageJS()
				}
			}
		})
	},
	back() {
		wx.navigateBack()
	},
	goDetail(index) {
		// console.log("查看本日消费账单", index, this.data.dateMonthIdx)
		let file = this.data.list[index]
		let monthFileName = null
		if (!['', null, undefined, false].includes(this.data.dateMonthIdx)) {
			monthFileName = this.data.dateMonthList[this.data.dateMonthIdx]
		}
		let monthParam = monthFileName ? '&month=' + monthFileName : ''
		wx.navigateTo({
			url: '/packageB/pages/day/index?file=' + file + monthParam,
		})
	},
	// 删除项目的函数（示例）
	deleteItem(index) {
		let file = this.data.list[index]
		wx.showModal({
			title: '提示',
			content: `是否确认删除${file}账单嘛`,
			complete: res => {
				if (res.cancel) {

				}

				if (res.confirm) {
					fileManager.removeFileJS(file)
					let list = this.data.list;
					list.splice(index, 1);
					this.setData({
						list: list,
						longPressIndex: null // 重置长按索引
					});
					wx.showToast({
						title: `${file}账单删除成功`,
						icon: "none"
					})
				}
			}
		})
	},
})